package com.qzdsoft.erpcloud.service.sale.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.qzdsoft.erpcloud.domain.sale.SaleOrder;
import com.qzdsoft.erpcloud.mapper.sale.SaleOrderDetailMapper;
import com.qzdsoft.erpcloud.mapper.sale.SaleOrderMapper;
import com.qzdsoft.erpcloud.mapper.sale.vi.SaleOrderViEnMapper;
import com.qzdsoft.erpcloud.service.sale.SaleOrderService;
import com.qzdsoft.erpcloud.vo.sale.SaleOrderEditEn;
import com.qzdsoft.erpcloud.vo.sale.SaleOrderQueryCriteriaViEn;
import com.qzdsoft.erpcloud.vo.sale.SaleOrderViEn;
import com.qzdsoft.utils.DateUtil;
import com.qzdsoft.utils.entityUtil.UtilsEntity;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.sale.SaleOrderStatus;

@Service
public class SaleOrderServiceImpl implements SaleOrderService {

	private static final Logger logger = LoggerFactory.getLogger(SaleOrderServiceImpl.class);

	@Autowired
	private SaleOrderViEnMapper saleOrderViEnMapper;

	@Autowired
	private SaleOrderDetailMapper saleOrderDetailMapper;

	@Autowired
	private SaleOrderMapper saleOrderMapper;

	@Override
	public LayTableResponse<SaleOrderViEn> findSaleOrdersBy(SaleOrderQueryCriteriaViEn saleOrderQueryCriteriaViEn) {
		logger.debug("查询销售订单，参数：{}", saleOrderQueryCriteriaViEn);
		// 指定时间+1天
		if (!StringUtil.isEmpty(saleOrderQueryCriteriaViEn.getEndDate())) {
			saleOrderQueryCriteriaViEn
					.setEndDate(DateUtil.getSpecifiedDayAfter(saleOrderQueryCriteriaViEn.getEndDate()));
		}
		PageHelper.startPage(saleOrderQueryCriteriaViEn.getPage(), saleOrderQueryCriteriaViEn.getLimit());
		List<SaleOrderViEn> orderList = saleOrderViEnMapper.selectAll(saleOrderQueryCriteriaViEn);
		LayTableResponse<SaleOrderViEn> po = new LayTableResponse<SaleOrderViEn>(orderList);
		return po;
	}

	@Override
	public SaleOrderViEn findSaleOrderBy(Integer id) {
		return saleOrderViEnMapper.selectOneBy(id);
	}

	@Transactional
	@Override
	public Response<String> edit(SaleOrderEditEn saleOrderEditEn,Integer userId) {
		SaleOrder saleOrder = saleOrderMapper.selectByPrimaryKey(saleOrderEditEn.getId());
		if (saleOrder == null) {
			logger.error("编辑销售订单，根据id:{}查询到的销售订单为空", saleOrderEditEn.getId());
			throw new RuntimeException("根据id查询到的销售订单为空");
		}
		UtilsEntity.copyEntity(saleOrderEditEn, saleOrder);

		saleOrder.setModifyTime(new Date());
		saleOrder.setModifyId(userId);
		saleOrderMapper.updateByPrimaryKey(saleOrder);
		return new Response<String>(ResultEnum.SUCCESS);
	}

	@Transactional
	@Override
	public Response<SaleOrderViEn> reivew(Integer userid, Integer id) {
		SaleOrder record = saleOrderMapper.selectByPrimaryKey(id);
		if (record == null) {
			logger.error("审核销售订单，根据id:{}查询到的销售订单为空", id);
			throw new RuntimeException("根据id查询到的销售订单为空");
		}
		record.setReviewId(userid);
		record.setReviewTime(new Date());
		record.setReviewStatus(Integer.parseInt(SaleOrderStatus.HAS_REVIEW_CODE));
		int count = saleOrderMapper.updateByPrimaryKey(record);
		if (count == 1) {
			SaleOrderViEn saleOrderInfo = saleOrderViEnMapper.selectOneBy(id);
			return new Response<SaleOrderViEn>(ResultEnum.SUCCESS, saleOrderInfo);
		} else {
			return new Response<SaleOrderViEn>(ResultEnum.ERROR);
		}
	}

	@Transactional
	@Override
	public Response<SaleOrderViEn> cancelReview(Integer userid, Integer id) {
		SaleOrder record = saleOrderMapper.selectByPrimaryKey(id);
		if (record == null) {
			logger.error("弃审销售订单，根据id:{}查询到的销售订单为空", id);
			throw new RuntimeException("根据id查询到的销售订单为空");
		}
		record.setModifyId(userid);
		record.setReviewTime(new Date());
		record.setReviewStatus(Integer.parseInt(SaleOrderStatus.NOT_REVIEW_CODE));
		int count = saleOrderMapper.updateByPrimaryKey(record);
		if (count == 1) {
			SaleOrderViEn saleOrderViEn = saleOrderViEnMapper.selectOneBy(id);
			return new Response<SaleOrderViEn>(ResultEnum.SUCCESS, saleOrderViEn);
		} else {
			return new Response<SaleOrderViEn>(ResultEnum.ERROR);
		}
	}

	@Transactional
	@Override
	public Response<String> close(Integer id) {
		SaleOrder record = saleOrderMapper.selectByPrimaryKey(id);
		if (record == null) {
			logger.error("关闭销售订单，根据id:{}查询到的销售订单为空", id);
			throw new RuntimeException("根据id查询到的销售订单为空");
		}
		record.setReviewTime(new Date());
		record.setReviewStatus(Integer.parseInt(SaleOrderStatus.CLOSE_CODE));
		int count = saleOrderMapper.updateByPrimaryKey(record);
		if (count == 1) {
			return new Response<String>(ResultEnum.SUCCESS);
		} else {
			return new Response<String>(ResultEnum.ERROR);
		}
	}

	@Transactional
	@Override
	public Response<String> remove(Integer id) {
		int orderDelCount = saleOrderMapper.deleteByPrimaryKey(id);
		if (orderDelCount != 1) {
			logger.error("关闭销售订单，根据id:{}查询到的销售订单为空", id);
			throw new RuntimeException("根据id查询到的销售订单为空");
		}
		int count = saleOrderDetailMapper.deleteByBillid(id);
		logger.debug("根据billid：{}删除销售订单明细行数：{}", id, count);
		return new Response<String>(ResultEnum.SUCCESS);
	}

	@Transactional
	@Override
	public Response<String> add(SaleOrderViEn saleOrderViEn) {
		SaleOrder record=new SaleOrder();
		record.setBillNo(saleOrderViEn.getBillNo());
		//根据BillNo查询销售订单是已经有记录
		List<SaleOrder> list = saleOrderMapper.select(record);
		if(list.size()>0){
			logger.error("创建销售订单，根据BillNo:{}查询到的销售订单已经存在，不能创建", record.getBillNo());
			return new Response<>(500,"系统已有相同编号["+record.getBillNo()+"]的销售订单,不能创建！请修改销售订单编号。");
//			throw new RuntimeException("系统已有相同编号["+record.getBillNo()+"]的销售订单,不能创建！");
		}
		SaleOrder saleOrder = new SaleOrder();
		// 复制相同字段值
		UtilsEntity.copyEntityValues(saleOrderViEn, saleOrder);
		// 创建时间
		saleOrder.setCtime(new Date());
		// 单据状态
		saleOrder.setReviewStatus(Integer.parseInt(SaleOrderStatus.NOT_REVIEW_CODE));
		int i = saleOrderMapper.insert(saleOrder);
		if (i > 0) {
			return new Response<>(ResultEnum.SUCCESS);
		} else {
			return new Response<>(ResultEnum.ERROR);
		}
	}

	@Override
	public List<SaleOrder> selectReviewOrderByCompanyId(SaleOrder record) {
		return saleOrderMapper.select(record);
	}

	@Override
	public List<TypeInfo> queryBillNo(Integer companyId) {
		return saleOrderViEnMapper.queryBillNo(companyId);
	}

}
